﻿// ===============================================================================
// XCI.Parent 西安交通信息WinForm开发框架 2019 版权所有
// ===============================================================================

using System;
using XCI.Extensions;
using XCI.Sys.Model;
using XCI.Sys.Service;
using XCI.Windows;
using XCI.Windows.Helper;

namespace XCI.Sys.Forms
{
    /// <summary>
    /// 系统在线用户管理
    /// </summary>
    [FormCaption("系统在线用户管理")]
    public partial class FrmOnlineUserManager : FrmSysBase
    {
        /// <summary>
        /// 表格操作类
        /// </summary>
        private GridHelper<OnlineUserModel> helper;
        private readonly bool allowSelect = SysRuntime.IsAuthorize("sys.onlineUser.select");
        private readonly bool allowLogoff = SysRuntime.IsAuthorize("sys.param.logoff");
        //private readonly bool allowExport = SysRuntime.IsAuthorize("sys.param.export");

        public FrmOnlineUserManager()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 页面初始化
        /// </summary>
        private void InitForm()
        {
            helper = new GridHelper<OnlineUserModel>(gridView);
            helper.ElapsedTimeItem = labExecuteTime;

            LoadData();
            ButtonStatus();
        }

        /// <summary>
        /// 数据加载
        /// </summary>
        private void LoadData()
        {
            //权限控制
            if (!allowSelect) return;

            helper.RunCallback(e =>
            {
                e.Result = OnlineUserService.Instance.Query();
            })
            .CompletCallback(e =>
            {
                ButtonStatus();
            })
            .RunAsync();
        }

        /// <summary>
        /// 设置按钮可用状态
        /// </summary>
        private void ButtonStatus()
        {
            btnDelete.Enabled = helper.HasSelected && allowLogoff;
        }

        /// <summary>
        /// 页面加载事件
        /// </summary>
        private void FrmOnlineUserManager_Load(object sender, EventArgs e)
        {
            InitForm();
        }

        /// <summary>
        /// 表格选中项发生变化事件
        /// </summary>
        private void gridView_SelectionChanged(object sender, DevExpress.Data.SelectionChangedEventArgs e)
        {
            ButtonStatus();
        }

        /// <summary>
        /// 删除事件
        /// </summary>
        private void btnDelete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            helper.Delete(() =>
            {
                OnlineUserService.Instance.Logoff(helper.SelectedId).IfTrue(LoadData);
            });
        }

        private void BtnRefresh_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            LoadData();
        }

        private void EditName_EditValueChanged(object sender, EventArgs e)
        {
            gridView.ApplyFindFilter(editName.Text.Trim());
        }
    }
}